package cn.lixiaodan.hqlbuilder.ui;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JDesktopPane;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTextArea;

import org.jdesktop.application.SingleFrameApplication;

import cn.lixiaodan.hqlbuilder.gen.GeneratorFactory;
import cn.lixiaodan.hqlbuilder.gen.IGenerator;
import cn.lixiaodan.hqlbuilder.gen.JavaHqlGenerator;
import cn.lixiaodan.hqlbuilder.model.ClassModel;

/**
 * This code was edited or generated using CloudGarden's Jigloo
 * SWT/Swing GUI Builder, which is free for non-commercial
 * use. If Jigloo is being used commercially (ie, by a corporation,
 * company or business for any purpose whatever) then you
 * should purchase a license for each developer using Jigloo.
 * Please visit www.cloudgarden.com for details.
 * Use of Jigloo implies acceptance of these licensing terms.
 * A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
 * THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
 * LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
 */
/**
 * 
 */
public class NewSingleFrameApplication extends SingleFrameApplication {
	private JPanel topPanel;
	private JDesktopPane jDesktopPane1;
	private JPanel jPanel1;
	private JButton jButton1;
	private JScrollPane jScrollPane2;
	private JSplitPane jSplitPane2;
	private JSplitPane jSplitPane1;
	private JPanel jPanel3;
	private JButton jButton3;
	private JButton jButton2;
	private JTextArea jTextArea1;
	private JPanel jPanel2;
	private SelectClass selectClass;
	public List<ClassInternalFrame> ciflist = new ArrayList<ClassInternalFrame>();
	public List<ConditionPane> cplist = new ArrayList<ConditionPane>();

	@Override
	protected void startup() {
		{
			// getMainFrame().setSize(658, 431);
			getMainFrame().setExtendedState(JFrame.MAXIMIZED_BOTH);
			getMainFrame().setLocation(0, 0);
		}
		topPanel = new JPanel();
		BorderLayout topPanelLayout = new BorderLayout();
		topPanel.setLayout(topPanelLayout);
		topPanel.setPreferredSize(new java.awt.Dimension(500, 300));
		{
			jSplitPane2 = new JSplitPane();
			jSplitPane2.setOrientation(JSplitPane.VERTICAL_SPLIT);
			topPanel.add(jSplitPane2);
			jSplitPane2.setPreferredSize(new java.awt.Dimension(121, 15));
			jSplitPane2.setName("jSplitPane2");
			{
				jPanel1 = new JPanel();
				jSplitPane2.add(jPanel1, JSplitPane.BOTTOM);
				BorderLayout jPanel1Layout = new BorderLayout();
				jPanel1.setLayout(jPanel1Layout);
				jPanel1.setPreferredSize(new java.awt.Dimension(779, 137));
				{
					jPanel3 = new JPanel();
					BoxLayout jPanel3Layout = new BoxLayout(jPanel3, javax.swing.BoxLayout.Y_AXIS);
					jPanel1.add(jPanel3, BorderLayout.WEST);
					jPanel3.setLayout(jPanel3Layout);
					{
						jButton1 = new JButton();
						jPanel3.add(jButton1);
						jButton1.setName("jButton1");
						jButton1.setBounds(10, 10, 81, 23);
						jButton1.addActionListener(new ActionListener() {
							public void actionPerformed(ActionEvent evt) {
								jButton1ActionPerformed(evt);
							}
						});
					}
					{
						jButton2 = new JButton();
						jPanel3.add(jButton2);
						jButton2.setName("jButton2");
						jButton2.addActionListener(new ActionListener() {
							public void actionPerformed(ActionEvent evt) {
								jButton2ActionPerformed(evt);
							}
						});
					}
					{
						jButton3 = new JButton();
						jPanel3.add(jButton3);
						jButton3.setName("jButton3");
						jButton3.addActionListener(new ActionListener() {
							public void actionPerformed(ActionEvent evt) {
								jButton3ActionPerformed(evt);
							}
						});
					}
				}
				{
					jSplitPane1 = new JSplitPane();
					jSplitPane1.setOrientation(JSplitPane.HORIZONTAL_SPLIT);
					jPanel1.add(jSplitPane1, BorderLayout.CENTER);
					jSplitPane1.setSize(400, 292);
					jSplitPane1.setName("jSplitPane1");
					{
						jScrollPane2 = new JScrollPane();
						jSplitPane1.add(jScrollPane2, JSplitPane.RIGHT);
						jScrollPane2.setPreferredSize(new java.awt.Dimension(67, 137));
						{
							jTextArea1 = new JTextArea();
							jScrollPane2.setViewportView(jTextArea1);
							jTextArea1.setName("jTextArea1");
						}
					}
					{
						jPanel2 = new JPanel();
						jSplitPane1.add(jPanel2, JSplitPane.LEFT);
						BoxLayout jPanel2Layout = new BoxLayout(jPanel2, javax.swing.BoxLayout.Y_AXIS);
						jPanel2.setLayout(jPanel2Layout);
						jPanel2.setPreferredSize(new java.awt.Dimension(340, 79));
					}
				}
			}
			{
				jDesktopPane1 = new JDesktopPane();
				jSplitPane2.add(jDesktopPane1, JSplitPane.TOP);
				jDesktopPane1.setName("jDesktopPane1");
			}
		}
		{
			selectClass = new SelectClass(this);
			selectClass.setVisible(false);
		}
		show(topPanel);
	}

	public static void main(String[] args) {
		launch(NewSingleFrameApplication.class, args);
	}

	private void jButton1ActionPerformed(ActionEvent evt) {
		this.selectClass.setVisible(true);
		this.selectClass.getjTextField1().requestFocus();
		this.selectClass.getjTextField1().setText("");
	}

	public void afterSelectedClass(String clsname) {
		ClassInternalFrame clsframe = new ClassInternalFrame(clsname, this);
		clsframe.setName(clsname);
		clsframe.setVisible(true);
		clsframe.setTitle(clsframe.getClassModel().getShortName());
		clsframe.setBounds(200 * ciflist.size(), 0, 200, this.jDesktopPane1.getHeight());
		this.jDesktopPane1.invalidate();
		this.jDesktopPane1.add(clsframe);
		this.jDesktopPane1.validate();
		ciflist.add(clsframe);
	}

	private void jButton2ActionPerformed(ActionEvent evt) {
		ConditionPane condition = new ConditionPane(this);
		cplist.add(condition);
		condition.setSize(this.jPanel2.getWidth(), 30);
		condition.setVisible(true);
		this.jPanel2.invalidate();
		this.jPanel2.add(condition);
		this.jPanel2.validate();
	}

	/**
	 * 生成代码
	 * 
	 * @param evt
	 */
	private void jButton3ActionPerformed(ActionEvent evt) {
		//
		ClassModel[] cmarr = new ClassModel[ciflist.size()];
		for (int i = 0; i != ciflist.size(); i++) {
			ClassInternalFrame cif = ciflist.get(i);
			cmarr[i] = cif.getSelectedClassModel();
		}
		//
		String[] conditions = new String[cplist.size()];
		for (int i = 0; i != cplist.size(); i++) {
			conditions[i] = cplist.get(i).getExpr();
		}

		IGenerator gen = GeneratorFactory.build();
		String str = gen.make(cmarr,conditions);
		this.jTextArea1.setText(str);
	}
}
